package com.office.personalFolders.sharedFolder.controller;

import com.office.personalFolders.myFolder.pojo.OaFile;
import com.office.personalFolders.myFolder.pojo.OaFolder;
import com.office.personalFolders.myFolder.pojo.TbStaff;
import com.office.personalFolders.myFolder.service.impl.folderService;
import com.office.personalFolders.sharedFolder.service.impl.sharedFolderService;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


/**
 * @project_name: OA
 * @description:
 * @author: LaoWang
 * @date: 2022/10/30 9:31
 */

@Controller
@RequestMapping("/sharedFolder")
public class sharedFolderControl {
    @Autowired
    private sharedFolderService folderService;

    public sharedFolderService getFolderService() {
        return folderService;
    }
    public void setFolderService(sharedFolderService folderService) {
        this.folderService = folderService;
    }

    //进入我的文件夹页面
    @RequestMapping("/goMyFolderList")
    public String goMyFolderList(){
        return "personalFolder/sharedFolder/myFoldersList";
    }


    //根据条件查询所有的文件夹然后进行分页
    @RequestMapping("/myFolderList")
    @ResponseBody
    public PageModel<OaFolder> myFolderMain(OaFolder oaFolder, PageModel<OaFolder> pageModel, HttpSession session){
        TbStaff tbStaff=new TbStaff();
        tbStaff.setStaffId("1");
        tbStaff.setRoleName("admin");
        session.setAttribute("login",tbStaff);


        TbStaff login = (TbStaff) session.getAttribute("login");
        oaFolder.setFolderEmpId(login.getStaffId());
        oaFolder.setFolderUsable(0);
        oaFolder.setFolderShare(0);
        return folderService.getOaFolderListByFY(oaFolder,pageModel);
    }


    //点击查询跳转文件夹详情页面（即当前用户下当前文件的所有文件夹和文件）
    @RequestMapping("/goView")
    public String goView(){
        return "personalFolder/sharedFolder/myFoldersView";
    }
    @RequestMapping("/goMyFolderView")
    @ResponseBody
    public String goMyFolderView(OaFolder oaFolder,HttpSession session){
        session.setAttribute("folderId",oaFolder.getFolderId());
        return "true";
    }


    //返回上一级目录
    @RequestMapping("/goPreContext")
    @ResponseBody
    public String goPreContext(OaFolder oaFolder,HttpSession session){
        return folderService.goPreContext(oaFolder, session);
    }




    //查询当前用户下当前文件的所有文件夹和文件
    @RequestMapping("/folderAndFileList")
    @ResponseBody
    public List<Object> getOaFile(OaFolder oaFolder, OaFile oaFile,HttpSession session){
        return folderService.getOaFile(oaFolder,oaFile,session);
    }


    //点击新建文件夹跳转新建页面
    @RequestMapping("/goAddView")
    public String goAddView(){
        return "personalFolder/sharedFolder/folderAdd";
    }
    @RequestMapping("/goMyFolderAddView")
    @ResponseBody
    public String goMyFolderAddView(OaFolder oaFolder,HttpSession session){
        session.setAttribute("folderId",oaFolder.getFolderId());
        return "true";
    }




    //新建文件夹
    @RequestMapping("/addMyFolder")
    @ResponseBody
    public String addMyFolder(OaFolder oaFolder,HttpSession session){
        String folderId = (String) session.getAttribute("folderId");
        oaFolder.setFolderParentId(folderId);
        TbStaff login = (TbStaff) session.getAttribute("login");
        oaFolder.setFolderCreateby(login.getStaffId());
        oaFolder.setFolderEmpId(login.getStaffId());
        Date date = new Date();
        oaFolder.setFolderCreatetime(date);
        oaFolder.setFolderModifyby(login.getStaffId());
        oaFolder.setFolderModifytime(date);
        oaFolder.setFolderModifyby(login.getStaffId());
        Integer add = folderService.addFolder(oaFolder);
        if(add==1){
            return "true";
        }else return "false";
    }



    //点击修改文件夹跳转修改页面
    @RequestMapping("/goModifyView")
    public String goModifyView(){
        return "personalFolder/sharedFolder/folderModify";
    }
    @RequestMapping("/goMyFolderModifyView")
    @ResponseBody
    public String goMyFolderModifyView(OaFolder oaFolder,HttpSession session){
        session.setAttribute("folderIdUpdate",oaFolder.getFolderId());
        return "true";
    }



    //文件夹名称验证，更名
    @RequestMapping("/nameYZ")
    @ResponseBody
    public String nameYZ(OaFolder oaFolder,HttpSession session){
        String folderId = (String) session.getAttribute("folderIdUpdate");
        oaFolder.setFolderId(folderId);
        return folderService.folderNameYZ(oaFolder);
    }

    //用户验证，是否为系统管理员
    @RequestMapping("/loginYZ")
    @ResponseBody
    public String loginYZ(OaFolder oaFolder,HttpSession session){
        TbStaff tbStaff = (TbStaff) session.getAttribute("login");
        if(tbStaff.getRoleName().equals("admin")){
            return "true";
        }else {
            return "false";
        }
    }


    //查询当前文件夹的父级文件夹所剩大小
    @RequestMapping("/getRemainSize")
    @ResponseBody
    public String getRemainSize(OaFolder oaFolder,HttpSession session){
        String folderId = (String) session.getAttribute("folderIdUpdate");
        oaFolder.setFolderId(folderId);
        return folderService.getRemainSize(oaFolder);
    }




    //修改文件夹
    @RequestMapping("/updateMyFolder")
    @ResponseBody
    public String updateMyFolder(OaFolder oaFolder,HttpSession session){
        String folderId = (String) session.getAttribute("folderIdUpdate");
        oaFolder.setFolderId(folderId);
        TbStaff login = (TbStaff) session.getAttribute("login");
        oaFolder.setFolderModifyby(login.getStaffId());
        oaFolder.setFolderModifytime(new Date());
        Integer add = folderService.updateFolder1(oaFolder);
        if(add==1){
            return "true";
        }else return "false";
    }




    //删除文件夹
    @RequestMapping("/deleteMyFolder")
    @ResponseBody
    public String deleteMyFolder(OaFolder oaFolder){
        oaFolder.setFolderUsable(1);
        Integer add = folderService.delete(oaFolder);
        if(add==1){
            return "true";
        }else return "false";
    }



}
